package com.hepinghospital.user.ui.dialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseDialogFragment;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.QueryProtocolResponse;

import java.util.Objects;

/**
 * @time 2020/3/18 14:37
 */

public class RulesDialogFragment extends BaseDialogFragment {

    private ProgressBar progressBar;
    private String rulesType;

    public static RulesDialogFragment newInstance(String type) {
        Bundle bundle = new Bundle();
        bundle.putString("rulesType", type);
        RulesDialogFragment rulesDialogFragment = new RulesDialogFragment();
        rulesDialogFragment.setArguments(bundle);
        return rulesDialogFragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private WebView webView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_rules, container);
        Button btn_commit = view.findViewById(R.id.btn_commit);
        TextView rulesTitle = view.findViewById(R.id.title);
        progressBar = view.findViewById(R.id.progressBar);
        webView = view.findViewById(R.id.wv_rules_detail);
        assert getArguments() != null;
        rulesType = getArguments().getString("rulesType");

        if ("6".equals(rulesType)) {
            rulesTitle.setText("预约规则");
        } else if ("1".equals(rulesType)) {
            rulesTitle.setText("知情同意书");
        } else if ("3".equals(rulesType)) {
            rulesTitle.setText("法律声明及隐私权政策");
        } else if ("9".equals(rulesType)) {
            rulesTitle.setText("服务须知");
        }

        //可以设置webview没得 滚动条
        webView.setBackgroundColor(0);
//        webView.getBackground().setAlpha( 2 );
        webView.getSettings().setJavaScriptEnabled(false);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webView.getSettings().setTextZoom(400);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.getSettings().setUseWideViewPort(true);//设置webview显示屏幕宽度 不能滑动
        webView.getSettings().setLoadWithOverviewMode(true);

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    if (webView != null) {
                        progressBar.setVisibility(View.GONE);//加载完网页进度条消失
                    }

                } else {
                    if (webView != null) {
                        progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                        progressBar.setProgress(newProgress);//设置进度值
                    }


                }
                super.onProgressChanged(view, newProgress);
            }
        });

        btn_commit.setOnClickListener(v -> mListener.onConfirm());

        queryProtocol();
        return view;
    }

    private void queryProtocol() {
        RestProxy.getInstance().queryProtocol(rulesType, new BaseObserver<QueryProtocolResponse>() {
            @Override
            public void onSuccess(QueryProtocolResponse baseResponse) {
                webView.loadDataWithBaseURL(null, baseResponse.text, "text/html", "utf-8", null);
            }
        });
    }



    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
//        Objects.requireNonNull(Objects.requireNonNull(getDialog()).getWindow()).requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        Objects.requireNonNull(getDialog().getWindow()).setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


}
